﻿using GN.Pay.Utils;
using GN.Pay.Utils.Net.Http;
using GN.Pay.Utils.Security;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GN.Pay.TradeChannel.YJPay.Response
{
    [Serializable]
    public class QueryResponseEntity : AService
    {
        public static QueryResponseEntity Create(string responseString)
        {
            if (!string.IsNullOrEmpty(responseString))
            {
                return CommonExtension.JsonToObject(responseString, typeof(QueryResponseEntity), false) as QueryResponseEntity;
            }
            return null;
        }

        /// <summary>
        /// 处理笔数
        /// </summary>
        public int processCount { get; set; }

        /// <summary>
        /// 服务
        /// </summary>
        public string service { get; set; }

        /// <summary>
        /// 订单号
        /// </summary>
        public string orderNo { get; set; }


        /// <summary>
        /// 处理失败项数
        /// </summary>
        public int failCount { get; set; }

        /// <summary>
        /// 处理标识
        /// </summary>
        public string resultCode { get; set; }  

        /// <summary>
        /// 签名
        /// </summary>
        public string sign { get; set; }

        /// <summary>
        /// 总笔数
        /// </summary>
        public int totalCount { get; set; }

        /// <summary>
        /// 响应消息
        /// </summary>
        public string resultMessage { get; set; }

        /// <summary>
        /// 版本
        /// </summary>
        public string version { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public string protocol { get; set; }

        /// <summary>
        /// 返回标识
        /// </summary>
        public string success { get; set; }

        /// <summary>
        /// 处理成功总笔数
        /// </summary>
        public int successCount { get; set; }

        /// <summary>
        /// 签名方式
        /// </summary>
        public string signType { get; set; }

        /// <summary>
        /// 商户ID
        /// </summary>
        public string partnerId { get; set; }

        /// <summary>
        /// 买家付总额
        /// </summary>
        public string buyerTotalAmount { get; set; }

        /// <summary>
        /// 付款详情
        /// </summary>
        public List<singleTradeInfo> singleTradeInfos { get; set; }

        private const string BEGIN_INDEX_STR = "\"singleTradeInfos\":[";

        /// <summary>
        /// 签名验证
        /// </summary>
        /// <param name="responseString"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        internal bool SignVeryfy(string responseString, string key)
        {
            SignDictionary sortDic = this.GetSignDictionary(StringComparer.Ordinal);
            string sign;
            if (!sortDic.TryGetValue("sign", out sign) || string.IsNullOrWhiteSpace(sign))
            {
                return false;
            }
            sortDic.Remove("sign");
            sortDic.Remove("singleTradeInfos");
            var beginIndex = responseString.IndexOf(BEGIN_INDEX_STR);
            if (beginIndex >= 0)
            {
                beginIndex += BEGIN_INDEX_STR.Length - 1;
                var endIndex = responseString.LastIndexOf(']');
                if (endIndex > beginIndex)
                {
                    var srt = responseString.Substring(beginIndex, endIndex - beginIndex + 1);
                    sortDic["singleTradeInfos"] = srt;
                }
            }
            if (sortDic.Count == 0)
            {
                return false;
            }
            string content = sortDic.UrlLinkString() + key;            
            return Md5Utils.SginVerify(content, sign, Encoding.UTF8);
        }

    }


    public partial class singleTradeInfo
    {
        /// <summary>
        /// 商户号
        /// </summary>
        public string partnerId { get; set; }

        /// <summary>
        /// 已经退款
        /// </summary>
        public List<refundInfo> refundInfos { get; set; }
        /// <summary>
        /// 订单完成状态
        /// </summary>
        public string fastPayStatus { get; set; }

        /// <summary>
        /// 完成时间
        /// </summary>
        public DateTime finishTime { get; set; }

        /// <summary>
        /// 商品标题
        /// </summary>
        public string goodsName { get; set; }

        /// <summary>
        /// 订单号
        /// </summary>
        public string merchOrderNo { get; set; }

        /// <summary>
        /// 付款金额
        /// </summary>
        public decimal payAmount { get; set; }

        /// <summary>
        /// 付款方式
        /// </summary>
        public string payMethod { get; set; }

        /// <summary>
        /// 交易号
        /// </summary>
        public string tradeNo { get; set; }

        /// <summary>
        /// 是否付款成功
        /// </summary>
        /// <returns></returns>
        public bool IsPaySuccess()
        {
            return "FINISHED".Equals(fastPayStatus, StringComparison.InvariantCultureIgnoreCase)
                || "PAY_PROCESSING".Equals(fastPayStatus, StringComparison.InvariantCultureIgnoreCase);
        }
    }

    public partial class refundInfo
    {
        /// <summary>
        /// 退款单号
        /// </summary>
        public string merchOrderNo { get; set; }

        /// <summary>
        /// 退款金额
        /// </summary>
        public decimal refundAmount { get; set; }

        /// <summary>
        /// 退款状态
        /// </summary>
        public string refundStatus { get; set; }

        /// <summary>
        /// 交易订单号
        /// </summary>
        public string tradeNo { get; set; }
    }
}
